package com.cjc.card.greeting.service.impl;

import com.cjc.card.greeting.dto.TaskDto;
import com.cjc.card.greeting.entity.UserJZG;
import com.cjc.card.greeting.service.TaskService;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpMediaService;
import me.chanjar.weixin.cp.api.WxCpMessageService;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.api.impl.WxCpMediaServiceImpl;
import me.chanjar.weixin.cp.api.impl.WxCpMessageServiceImpl;
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
import me.chanjar.weixin.cp.bean.message.WxCpMessage;
import me.chanjar.weixin.cp.bean.message.WxCpMessageSendResult;
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

@Service
public class TaskServiceImpl implements TaskService {

    @Autowired
    private TaskDto taskDto;


    @Override
    public List<UserJZG> getUserByToDay() {

        UserJZG userJZG = new UserJZG();
        userJZG.setSex(1);
        userJZG.setSr("0503");
        userJZG.setYhh("JiaJunDe");

        List l = new ArrayList();
        l.add(userJZG);

        return l;
    }

    @Override
    public File getCard(UserJZG userJZG) {


        File file = new File("D:\\b\\1.png");
        return file;
    }

    @Override
    public boolean send(File cardFile, String yhh) {

        WxCpService service = new WxCpServiceImpl();

        WxCpConfigStorage storage = new WxCpDefaultConfigImpl();

        ((WxCpDefaultConfigImpl) storage).setCorpId("ww15c91198cab468c1");
        ((WxCpDefaultConfigImpl) storage).setAgentId(1000020);
        ((WxCpDefaultConfigImpl) storage).setCorpSecret("w6hqGZ3fEeU6LywKYEia3f_dx_j04TwHX_fNe7roWXs");

        service.setWxCpConfigStorage(storage);

        WxCpMediaService media = new WxCpMediaServiceImpl(service);
        try {
            WxMediaUploadResult result = media.upload("image",cardFile);
            String mediaId = result.getMediaId();


            WxCpMessageService message = new WxCpMessageServiceImpl(service);

            WxCpMessage cpMessage = new WxCpMessage();
            cpMessage.setToUser(yhh);
            cpMessage.setMsgType("image");
            cpMessage.setAgentId(1000020);
            cpMessage.setMediaId(mediaId);


            WxCpMessageSendResult result1 = message.send(cpMessage);

            if(result1.getErrCode()==0 && result1.getErrMsg().equals("ok")){
                System.out.println("发送成功");

                return  true;
            }

        } catch (WxErrorException e) {
            e.printStackTrace();
        }


        return false;
    }

    @Override
    public boolean task() {


        List<UserJZG>  userList = this.getUserByToDay();

        if (null != userList){

            for (UserJZG u : userList){

                File f = this.getCard(u);

                if (null != f){
                    this.send(f,u.getYhh());
                }

            }

        }

        return false;
    }
}
